package com.hema.auction.widget;

import android.content.Context;

import com.hema.auction.R;
import com.yanyusong.y_divideritemdecoration.Y_Divider;
import com.yanyusong.y_divideritemdecoration.Y_DividerBuilder;
import com.yanyusong.y_divideritemdecoration.Y_DividerItemDecoration;

/**
 * 作者:john
 */
public class GridSpaceItemDecoration extends Y_DividerItemDecoration {

    private int dividerColor;
    private int itemCount;
    private int space;
    private boolean hasHeader;

    public GridSpaceItemDecoration(Context context, int space) {
        super(context);
        dividerColor = context.getResources().getColor(R.color.colorDivider);
        this.space = space;
    }

    public void setItemCount(int itemCount) {
        this.itemCount = itemCount;
    }

    public void setHasHeader(boolean hasHeader) {
        this.hasHeader = hasHeader;
    }

    @Override
    public Y_Divider getDivider(int itemPosition) {
        Y_Divider divider = null;
        if (hasHeader) {
            if (itemPosition == 0 || itemPosition == itemCount - 1) {
                divider = new Y_DividerBuilder()
                        .create();
                return divider;
            }
        } else {
            if (itemPosition == itemCount - 1) {
                divider = new Y_DividerBuilder()
                        .create();
                return divider;
            }

        }
        switch (itemPosition % 2) {
            case 1:
                if (hasHeader) {
                    //每一行第一个显示right和bottom
                    divider = new Y_DividerBuilder()
                            .setRightSideLine(true, dividerColor, space, 0, 0)
                            .setBottomSideLine(true, dividerColor, space, 0, 0)
                            .create();
                } else {
                    //第二个显示bottom
                    divider = new Y_DividerBuilder()
                            .setBottomSideLine(true, dividerColor, space, 0, 0)
                            .create();
                }
                break;
            case 0:
                if (hasHeader) {
                    //第二个显示bottom
                    divider = new Y_DividerBuilder()
                            .setBottomSideLine(true, dividerColor, space, 0, 0)
                            .create();
                } else {
                    //每一行第一个显示right和bottom
                    divider = new Y_DividerBuilder()
                            .setRightSideLine(true, dividerColor, space, 0, 0)
                            .setBottomSideLine(true, dividerColor, space, 0, 0)
                            .create();
                }
                break;

        }
        return divider;
    }

}